﻿
// #define DEBUG_DRAW

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace Tank
{
    public class Graphic2D
    {
        private Vector2 position;           // Upper-left hand coord of graphic box
        private Dictionary<string, string> frameToTextureMap;
        private Dictionary<string, Rectangle> frameMap;
        private string currFrame;
        private string currTexture;
        private Rectangle destRect;
        private Color color;

        public Graphic2D(Vector2 pos, Vector2 szWdHgt)
        {
            position = new Vector2(pos.X, pos.Y);
            destRect = new Rectangle((int)(position.X - szWdHgt.X * 0.5f), (int)(position.Y - szWdHgt.Y * 0.5f), (int)szWdHgt.X, (int)szWdHgt.Y);
            frameToTextureMap = new Dictionary<string, string>();                   // Mapping from frameIDs to texturesIDs
            currTexture = "";
            frameMap = new Dictionary<string, Rectangle>();
            color = Color.White;
        }

        public void AddFrame(string textureID, string frameID, Rectangle sourceRect, bool setCurrent)
        {
            if (frameMap.Count <= 0)
            {
                currFrame = frameID;
                currTexture = textureID;
            }

            frameMap[frameID] = sourceRect;
            frameToTextureMap[frameID] = textureID;

            if (setCurrent)
            {
                currFrame = frameID;
                currTexture = textureID;
            }
        }

        public void SetFame(string stringID)
        {
            currFrame = stringID;
        }

        public void Draw(SpriteBatch sb)
        {
            Texture2D texture = World.Instance.GetTexture(currTexture);
            destRect.X = (int)(position.X - destRect.Width * 0.5f);
            destRect.Y = (int)(position.Y - destRect.Height * 0.5f);
            sb.Draw(texture, destRect, frameMap[currFrame], color);


#if DEBUG_DRAW
            Texture2D texture2 = World.Instance.GetTexture("DebugCircle");
            sb.Draw(texture2, new Vector2(destRect.X, destRect.Y), Color.White);
            sb.Draw(texture2, new Vector2(destRect.X + destRect.Width, destRect.Y), Color.White);
            sb.Draw(texture2, new Vector2(destRect.X, destRect.Y + destRect.Height), Color.White);
            sb.Draw(texture2, new Vector2(destRect.X + destRect.Width, destRect.Y + destRect.Height), Color.White);

#endif // DEBUG_DRAW

        }

        public void Draw(SpriteBatch sb, Texture2D texture2D)
        {
            Debug.Assert(frameToTextureMap.Count > 0);
            Vector2 drawPos = new Vector2(position.X - destRect.Width * 0.5f, position.Y - destRect.Height * 0.5f);
            sb.Draw(texture2D, drawPos, color);
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Color BaseColor
        {
            set { color = value; }
        }

        public string GetTextureID(string index)
        {
            Debug.Assert(frameToTextureMap[index] != null);
            return frameToTextureMap[index];
        }

        public string CurrentTextureID
        {
            get { return currTexture; }
        }

        public string FrameID
        {
            get { return currFrame; }
            set { currFrame = value; }
        }

        public Rectangle DestinationRect
        {
            get { return destRect; }
            set { destRect = value; }
        }

        public Rectangle SourceRect
        {
            get { return frameMap[currFrame]; }
        }
    }
}
